from __future__ import absolute_import
#from os.path import join, exists
#import sys
#import os
#import logging
from forest.api import Command, validators, get_tool, Config
#from forest.commands.yum.install import Install
#from forest.api import validators

class EmbedTree(object):
    ''' Embeds a tree file with the image/iso. '''
    __register__ = 'commands.forest.EmbedTree'

    requires = ['yum.Mirror']

    # TODO: refactor into
    def get_iso_root(self):
        return os.path.join(self._config.get_build_root(), 'isoroot')

    def join_iso_root(self, *args):
        return os.path.join(self._config.get_build_root(), 'isoroot', *args)

    def run(self, config):
        'DEPRECATED'
        return
        embed_file = config.get('embed_tree', False)

        # fetch the default configuration file to include with the installer image
        if not embed_file:
            return

        #embed_source = str(embed_source)

        installyaml = embed_file

        if embed_file is None:
            raise Exception("Could not find configuration source %s relative to %s" % (embed_file, config.url.geturl()))

        Command.SIGNAL_COMMAND_PROGRESS(self, message='Copying built in install.yaml from %s' % (installyaml))

        # if config is_remote, then install.tree will also be remote,
        # We need to let the Config object know this
        realconfigurl = config.translate_path(embed_file)

        print 'Real config url', realconfigurl.geturl()

        install_config = Config()
        install_config.load_file(realconfigurl.geturl())

        get_tool('system.mkdir')(self.get_iso_root())

        install_config.set('yum.repos', [{'name':'Forest', 'url':'file:///mnt/install/Packages'}])

        get_tool('files.write')(self.join_iso_root('install.tree'), install_config.serialize())
